<?php

class Home extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model("mindex");
        $this->load->model("products_ads_model",'ads');
        $this->load->model("category_model",'category');
        $this->load->model('macdinh');
    }

    public function index() {   
        $data["top16post"] = $this->mindex->get16post();
        $data["top5media"] = $this->mindex->get5media();
        $data["top5picture"] = $this->mindex->get5picture();
        $data["top3video"] = $this->mindex->get3video();
        //$data["topic"] = $this->mindex->gettop();
        $data["topicReadless"] = $this->mindex->getTopicReadless();
        $this->loadMainHeader();
        $data['categories'] = $this->category->listAll();
        $data['sub_categories'] = $this->category->listSubCate();
        $this->load->view('default/index',$data);
        $this->loadMainFooter();
    }
    function loadMainHeader() {
        $data['menu'] = $this->category->listAll();
        $data['submenu'] = $this->category->listSubCate();
        $this->load->view('default/header',$data);
    }
    function loadMainFooter() {
        $data['categories'] = $this->category->listAll();
        $data['sub_categories'] = $this->category->listSubCate();
        $data['products_ads'] = $this->ads->listAd();
        $this->load->view('default/footer', $data);
    }
    function category($link){
        $link = trim($link);
        $cate_info= $this->category->getInfoByLink($link);
        $data['cate_info'] = $cate_info;
        $data['listsub_cate'] = $this->category->listSubCateById($cate_info->category_id);
        $this->loadMainHeader();
        $this->load->view('default/category',$data);
        $this->loadMainFooter();
    }
    function sub_cate($cate_class,$sub_cate_link){
        $sub_info = $this->category->getSubInfoByLink($sub_cate_link);
        if(!$sub_info){
            redirect('home');
        }
        $data['sub_info'] = $sub_info;
        $cate_info = $this->category->getInfoByClass($cate_class);
        $data['cate_info'] = $cate_info;
        $data['category_name'] = $cate_info->category_name;
        $data['listTopicBySubId'] = $this->mindex->listTopicBySubId($sub_info->sub_category_id);
        $this->loadMainHeader();
        $this->load->view('default/sub_category',$data);
        $this->loadMainFooter();
    }
    function getNew(){
        $top5new= $this->mindex->get5new();
        foreach ($top5new as $key=>$value) {
            $array[] = $this->arrayToObject(array(
                'link' =>site_url('home/detail').'/'.$value->topic_news_id,
                'summary'=>substr(strip_tags($value->topic_news_content), 0,250).'...',
                'title'=>$value->topic_news_title,
                'cover'=>base_url().$value->topic_news_picture,
                'time'=>$value->topic_news_date,
                'catechild'=>$value->sub_category_id,
                'cateparent'=>$value->category_id,
                'type'=>$value->topic_news_type

            ));
        }
        $arrayTop = array('server_time' =>time() ,'data'=>$array);
        return $arrayTop;
    }
    function arrayToObject($array){
            foreach ($array as $key => $value) {
                if(is_array($value)){
                    $array[$key] = arrayToObject($value);
                }
            }
            return (object) $array;
    }
    function detail($id){
        $data["top5new"] = $this->mindex->get5new();
        $topicInfo = $this->mindex->getInfoTopic($id);
        $sub_info = $this->category->getSubInfo($topicInfo->sub_category_id);
        $data['sub_cate_name'] = $sub_info->sub_category_name;
        $data["readMore5topic"] = $this->mindex->get5TopicBySubId($sub_info->sub_category_id,$id);
        $data["topicTag"] = $this->mindex->getTopicTag($topicInfo->topic_news_id,$topicInfo->topic_news_tag);
        $data["topicReadless"] = $this->mindex->getTopicReadlessBySub($sub_info->sub_category_id);
        $data['topicInfo'] = $topicInfo;
        $data['related_cate'] = $this->category->related_cate($topicInfo->category_id);
        $this->mindex->updateCountView($id);
        $this->loadMainHeader();
        $this->load->view('default/detail',$data);
        $this->load->view('default/footer_mini');
    }
    function search($keyword){
        if (!isset($keyword) || isset($keyword) && $keyword == '')
            redirect('home');
        parse_str($_SERVER['QUERY_STRING'], $get);
        if(count($get) != 1) {
            redirect('home');
        }
        if(!isset($get['type'])) {
            redirect('home');
        }
        $type = $get['type'];
        $keyword = urldecode($keyword);
        $this->loadMainHeader();
        $limit = 5;
        $start = 0;
        $result = $this->mindex->getSearch($keyword,$type,$start, $limit);
        $total = $this->mindex->totalgetSearch($keyword,$type);
        $data["topicReadless"] = $this->mindex->getTopicReadless();
        //$data['result_search_keyword'] = 'Hiện có '.$total.' kết quả tìm kiếm với từ khóa : "'.$keyword.'"';
        $data['start'] = $start;
        $data['limit'] = $limit;
        $data['total'] = $total;
        $data['type'] = $type;
        $data['keyword'] = $keyword;
        $data['page'] = 0;
        $data['searchTopic'] = $result;
        $this->load->view('default/search_view', $data);
        $this->loadMainFooter();
    }
    function moreSearch($keyword,$page){
        if (!isset($keyword) || isset($keyword) && $keyword == '')
            exit(json_encode(array('status' => false )));
        parse_str($_SERVER['QUERY_STRING'], $get);
        if(count($get) != 1) {
            exit(json_encode(array('status' => false )));
        }
        if(!isset($get['type'])) {
            exit(json_encode(array('status' => false )));
        }
        $type = $get['type'];
        $keyword = urldecode($keyword);
        $limit = 5;
        $start = $limit * $page;
        $result = $this->mindex->getSearch($keyword,$type,$start, $limit);
        if($result){
            $list['searchTopic']= $result;
            $data = $this->load->view('default/search_more', $list, true);
            exit(json_encode(array('status' =>true ,'list'=>$data)));
        }else{
            exit(json_encode(array('status' => false )));
        }
    }
}